<template>
    <div class="check">
        <dialog-task-details
                :driver-data="driverData"
                :task-detail="taskDetailData"
                v-on:complete="completeTask"
                v-on:reDispatch="reDispatchTask"
                v-on:cancel="cancelTask">
        </dialog-task-details>
    </div>
</template>

<script>
    import DialogTaskDetails from '../../components/dialogModel/TaskDetails.vue';
    export default {
        name: 'task-detail',
        props: {
            params: {
                type: Object,
                default: () => {
                    return {}
                }
            }
        },
        components: {
            DialogTaskDetails
        },
        data() {
            return {
                taskDetailData: {},
                driverData: {}
            }
        },
        computed: {
            // model: function (value) {
            //     return this.params.model
            // },
            dataRefreshDialog() {
                return this.$store.state.num;
            },
            id: function (value) {
                return this.params.id
            }
        },
        watch: {
            dataRefreshDialog: function () {
                this.getTaskDetail();
            },
        },
        created() {
            this.$emit('dialogTitle','任务详情');
            this.getTaskDetail();
        },
        methods: {
            getDriverData(driver_id) {
                this.$http.get('express/driver_record/' + driver_id)
                    .then((response)=>{
                        this.driverData = response.data.data;
                    })
                    .catch((error) => {
                        if (error.response.data.msg) {
                            this.$message.error(error.response.data.msg);
                        } else {
                            this.$message.error('请求错误：' + error.response.status);
                        }
                    });
            },
            getTaskDetail() {
                this.$http.get('express/mission/' + this.id)
                    .then((response) => {
                        this.taskDetailData = response.data.data;
                        this.getDriverData(this.taskDetailData.driver_id);
                    })
                    .catch((error) => {
                        if (error.response.data.msg) {
                            this.$message.error(error.response.data.msg);
                        } else {
                            this.$message.error('请求错误：' + error.response.status);
                        }
                    });

            },
            completeTask() {
                this.$http.put('express/mission/' + this.id + '/complete')
                    .then((response) => {
                        this.$emit('closeDialog');
                        this.$store.commit('refreshDialog');
                        this.$message({
                            message: '操作成功！',
                            type: 'success'
                        });
                    })
                    .catch((error) => {

                        if (error.response.data.msg) {
                            this.$message.error(error.response.data.msg);
                        } else {
                            this.$message.error('请求错误：' + error.response.status);
                        }
                    });
            },
            reDispatchTask(val, driver_id) {
                // let params = {
                //     mission_id: val,
                //     re_dispatch: 're_dispatch'
                // };
                this.$router.push({path:'/dialog/assign/express.order/' + val + '/' + driver_id});
            },
            cancelTask() {
                this.$http.put('express/mission/' + this.id + '/cancel')
                    .then((response) => {
                        this.$emit('closeDialog');
                        this.$store.commit('refreshDialog');
                        this.$message({
                            message: '操作成功！',
                            type: 'success'
                        });
                    })
                    .catch((error) => {

                        if (error.response.data.msg) {
                            this.$message.error(error.response.data.msg);
                        } else {
                            this.$message.error('请求错误：' + error.response.status);
                        }
                    });
            }
        }
    }
</script>

<style scoped>

</style>
